package com.netafull.adapter;

import java.util.HashMap;
import java.util.List;

import com.netafull.R;
import com.netafull.analyze.News;
import com.netafull.util.AsyncImageLoader;
import com.netafull.util.AsyncImageLoader.ImageCallback;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HomePageAdapter extends BaseAdapter {

	public List<News> items;
	Context context;
	public HashMap<Integer, Drawable> hm;
	private AsyncImageLoader asyncImageLoader;
	int count = 0;
	
	public int flag = 0;

	public HomePageAdapter(List<News> items, Context context) {
		super();
		this.items = items;
		this.context = context;
		hm = new HashMap<Integer, Drawable>();
		asyncImageLoader = new AsyncImageLoader();
		count = items.size();
	}

	@Override
	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	@Override
	public Object getItem(int arg0) {
		return items.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		News temp = items.get(position);

		View view = null;
		final ViewHolder holder = new ViewHolder();
		String image_url = temp.getImg_url();
		// System.out.println(image_url);
		if (image_url != null) {

			view = LayoutInflater.from(context).inflate(
					R.layout.homepage_list_item, null);
			holder.tv_public_date = (TextView) view
					.findViewById(R.id.list_public_date);
			holder.tv_public_date.setText(temp.getPublic_date());
			holder.tv_title = (TextView) view.findViewById(R.id.list_title);
			holder.tv_title.getPaint().setFakeBoldText(true);
			holder.tv_title.setText(temp.getTitle());
			holder.tv_description = (TextView) view
					.findViewById(R.id.list_description);
			holder.tv_description.setText(temp.getDescription());
			holder.iv_image = (ImageView) view.findViewById(R.id.list_image);

			// 动态加载图片

			Drawable d = hm.get(position);
			if (d != null) {
				holder.iv_image.setImageDrawable(d);
			} else {
				asyncImageLoader.loadDrawable(image_url, new ImageCallback() {

					@Override
					public void imageLoaded(Drawable imageDrawable,
							String imageUrl) {
						// TODO Auto-generated method stub
						if (holder.iv_image != null && imageDrawable != null) {
							holder.iv_image.setImageDrawable(imageDrawable);
							hm.put(position, imageDrawable);
							//System.out.println("position=" + position + "yes");
						}else{
							if(flag>3){
								holder.iv_image.setImageResource(R.drawable.noimage);
								hm.put(position, holder.iv_image.getDrawable());
							}
							flag++;
						}
					}
				});
			}
		} else {
			view = LayoutInflater.from(context).inflate(
					R.layout.homepage_list_item_noimage, null);
			holder.tv_public_date = (TextView) view
					.findViewById(R.id.list_public_date_noImage);
			holder.tv_public_date.setText(temp.getPublic_date());
			holder.tv_title = (TextView) view
					.findViewById(R.id.list_title_noImage);
			holder.tv_title.getPaint().setFakeBoldText(true);
			holder.tv_title.setText(temp.getTitle());
			holder.tv_description = (TextView) view
					.findViewById(R.id.list_description_noImage);
			holder.tv_description.setText(temp.getDescription());
		}

		return view;
	}

	public static class ViewHolder {
		TextView tv_public_date;
		TextView tv_title;
		TextView tv_description;
		ImageView iv_image;
	}
}
